//
//  HDButton.m
//
//  Created by HaoDong chen on 2019/4/24.
//  Copyright © 2019 chd All rights reserved.
//

#import "HDButton.h"

static NSInteger titleWidthOffset = 2;

@implementation HDButton {
    HDButtonImagePositonType _type;
    UIEdgeInsets _contentInset;
    CGFloat _space;
}

@synthesize titleL = _titleL;
@synthesize imageV = _imageV;

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
    CGRect newRect = CGRectMake(-self.expandClickInset.left,
                                -self.expandClickInset.top,
                                CGRectGetWidth(self.frame)+self.expandClickInset.left+self.expandClickInset.right,
                                CGRectGetHeight(self.frame)+self.expandClickInset.top+self.expandClickInset.bottom);
    BOOL isContain = CGRectContainsPoint(newRect, point);
    return isContain;
}

- (instancetype)init {
    if ([super init]) {
        _type = HDButtonImageLeft;
        _contentInset = UIEdgeInsetsZero;
        _space = 0;
        _imageSize = CGSizeZero;
        
        [self addSubview:self.titleL];
        [self addSubview:self.imageV];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        _type = HDButtonImageLeft;
        _contentInset = UIEdgeInsetsZero;
        _space = 0;
        
        [self addSubview:self.titleL];
        [self addSubview:self.imageV];
    }
    return self;
}

- (UILabel *)titleL {
    if (!_titleL) {
        _titleL = [UILabel new];
    }
    return _titleL;
}

- (UIImageView *)imageV {
    if (!_imageV) {
        _imageV = [UIImageView new];
    }
    return _imageV;
}

- (void)setNeedUpdate {
    [self invalidateIntrinsicContentSize];
    [self setNeedsLayout];
}

- (void)setAttStr:(NSAttributedString *)attStr image:(UIImage *)image imagePositonType:(HDButtonImagePositonType)type space:(CGFloat)space {
    self.titleL.attributedText = attStr;
    [self.imageV setImage:image];
    _type = type;
    _space = space;
    [self setNeedUpdate];
}

- (void)setAttStr:(NSAttributedString *)attStr {
    self.titleL.attributedText = attStr;
    [self invalidateIntrinsicContentSize];
    [self setNeedUpdate];
}

- (void)setImg:(UIImage *)image {
    [self.imageV setImage:image];
    [self setNeedUpdate];
}

- (void)setSpace:(CGFloat)space {
    _space = space;
    [self setNeedUpdate];
}

- (void)setPostionType:(HDButtonImagePositonType)type {
    _type = type;
    [self setNeedUpdate];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    [self updateSubViewFrame];
}

- (void)setContentInset:(UIEdgeInsets)inset {
    _contentInset = inset;
    [self setNeedUpdate];
}

- (void)updateSubViewFrame {
    CGSize titleSize = [_titleL.attributedText boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 5555) options:0 context:nil].size;
    //设置宽度多出2像素，文本刚好显示时不会出现  ...
    if (_titleL.attributedText.string.length>0) {
        titleSize = CGSizeMake(titleSize.width+titleWidthOffset, titleSize.height);
    }
    CGSize imageVSize = [self imgNewSize:_imageV.image.size];
    
    CGFloat imageX = 0;
    CGFloat imageY = 0;
    CGFloat titleX = 0;
    CGFloat titleY = 0;
    CGSize fitSize = [self properSize];
    
    CGFloat spaceUse = _space;
    if (titleSize.width == 0 || imageVSize.width == 0) {
        spaceUse = 0;
    }
    CGFloat widthOffset = (CGRectGetWidth(self.frame) - fitSize.width)/2;
    CGFloat heightOffset = (CGRectGetHeight(self.frame) - fitSize.height)/2;
    switch (_type) {
        case HDButtonImageLeft:
        {
            imageX = MAX(_contentInset.left, MAX(0, _contentInset.left + widthOffset));
            titleX = imageX + imageVSize.width + spaceUse;
            
            imageY= (CGRectGetHeight(self.frame) - imageVSize.height)/2;
            titleY = (CGRectGetHeight(self.frame) - titleSize.height)/2;
            
            //压缩lable的width
            if (titleX + titleSize.width>CGRectGetWidth(self.frame)) {
                titleSize = CGSizeMake(CGRectGetWidth(self.frame)-titleX, titleSize.height);
            }
        }
            break;
        case HDButtonImageRight:
        {
            titleX = MAX(_contentInset.left, MAX(0, _contentInset.left + widthOffset));
            if (fitSize.width>CGRectGetWidth(self.frame)) {
                //压缩label的width
                titleSize = CGSizeMake(CGRectGetWidth(self.frame) - _contentInset.left - _contentInset.right - spaceUse - imageVSize.width, titleSize.height);
            }
            imageX = titleX + titleSize.width + spaceUse;
            
            imageY= (CGRectGetHeight(self.frame) - imageVSize.height)/2;
            titleY = (CGRectGetHeight(self.frame) - titleSize.height)/2;
            
        }
            break;
        case HDButtonImageTop:
        {
            imageY = MAX(_contentInset.top, MAX(0, _contentInset.top + heightOffset));//MAX(0, _contentInset.top + (CGRectGetHeight(self.frame)-fitSize.height)/2);
            titleY = imageY + imageVSize.height + spaceUse;
            
            imageX = (CGRectGetWidth(self.frame) - imageVSize.width)/2;
            titleX = (CGRectGetWidth(self.frame) - titleSize.width)/2+titleWidthOffset/2.0;
            
            if (titleX + titleSize.width>CGRectGetWidth(self.frame)) {
                titleSize = CGSizeMake(CGRectGetWidth(self.frame)-titleX, titleSize.height);
            }

        }
            break;
        case HDButtonImageBottom:
        {
            titleY = MAX(_contentInset.top, MAX(0, _contentInset.top + heightOffset));
            imageY = titleY + titleSize.height + spaceUse;
            
            imageX = (CGRectGetWidth(self.frame) - imageVSize.width)/2+titleWidthOffset/2.0;
            titleX = (CGRectGetWidth(self.frame) - titleSize.width)/2;
            
            if (titleX + titleSize.width>CGRectGetWidth(self.frame)) {
                titleSize = CGSizeMake(CGRectGetWidth(self.frame)-titleX, titleSize.height);
            }
    
        }
            break;
        default:
            break;
    }
    CGRect titleFrame = {floor(titleX+titleWidthOffset/2),titleY,titleSize};
    CGRect imageFrame = {floor(imageX),imageY,imageVSize};
//    if (CGSizeEqualToSize(imageFrame.size, CGSizeZero)) {
//        CGRect newTitleFrame = titleFrame;
//        newTitleFrame.origin.x += titleWidthOffset;
//        titleFrame = newTitleFrame;
//    }
    self.titleL.frame = titleFrame;
    self.imageV.frame = imageFrame;
    
    if (self.resetCenter && (_type == HDButtonImageLeft || _type == HDButtonImageRight)) {
        CGPoint center = self.imageV.center;
        center.y = self.titleL.center.y;
        self.imageV.center = center;
    }
    
    if (self.resetCenter && (_type == HDButtonImageTop || _type == HDButtonImageBottom)) {
        CGPoint center = self.imageV.center;
        center.x = self.titleL.center.x;
        self.imageV.center = center;
    }
}

- (CGSize)intrinsicContentSize {
    CGSize properSize = [self properSize];
    return properSize;
}

- (CGSize)imgNewSize:(CGSize)imgSize {
    if (!CGSizeEqualToSize(self.imageSize, CGSizeZero)) {
        return self.imageSize;
    }
    return imgSize;
}

- (CGSize)properSize {
    CGSize titleSize = [_titleL.attributedText boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 555) options:0 context:nil].size;
    if (_titleL.attributedText.string.length>0) {
        titleSize = CGSizeMake(titleSize.width+titleWidthOffset, titleSize.height);
    }
    CGSize imageVSize = [self imgNewSize:_imageV.image.size];
    
    CGFloat spaceUse = _space;
    if (titleSize.width == 0 || imageVSize.width == 0) {
        spaceUse = 0;
    }
    CGSize finalSize = CGSizeZero;
    CGFloat contentOffsetLR = _contentInset.left + _contentInset.right;
    CGFloat contentOffsetTB = _contentInset.top + _contentInset.bottom;
    
    switch (_type) {
        case HDButtonImageLeft:
        {
            finalSize = CGSizeMake(contentOffsetLR+imageVSize.width+titleSize.width+spaceUse, MAX(imageVSize.height+contentOffsetTB, titleSize.height+contentOffsetTB));
        }
            break;
        case HDButtonImageRight:
        {
            finalSize = CGSizeMake(contentOffsetLR+imageVSize.width+titleSize.width+spaceUse, MAX(imageVSize.height+contentOffsetTB, titleSize.height+contentOffsetTB));
            
        }
            break;
        case HDButtonImageTop:
        {
            finalSize = CGSizeMake(MAX(imageVSize.width+contentOffsetTB, titleSize.width+contentOffsetLR),contentOffsetTB+imageVSize.height+titleSize.height+spaceUse);
        }
            break;
        case HDButtonImageBottom:
        {
            finalSize = CGSizeMake(MAX(imageVSize.width+contentOffsetTB, titleSize.width+contentOffsetLR),contentOffsetTB+imageVSize.height+titleSize.height+spaceUse);
        }
            break;
        default:
            break;
    }

    return finalSize;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end

